﻿using MQTTnet.Server;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Linq;
using Z.EntityFramework.Plus;
using Masuit.Tools.Security;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using adb.excel;
using System.IO;
using Masuit.Tools;
using Microsoft.Net.Http.Headers;
using adb;
using System;
using Microsoft.Extensions.Caching.Memory;

namespace SensorCollection
{

    public class AlarmJsonModel
    {
        /// <summary>
        /// 警告值
        /// </summary>
        public decimal alarm { get; set; }
    }

    /// <summary>
    /// 警报数据上传
    /// </summary>
    public class AlarmDevice : IDataDealwith
    {
        private MqttServer mqttServer;
        private DbContextOptionsBuilder<DeviceDbContext> optionsBuilder;
        private DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder;
        public AlarmDevice(MqttServer mqttServer, DbContextOptionsBuilder<DeviceDbContext> optionsBuilde, DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder)
        {
            this.mqttServer = mqttServer;
            this.optionsBuilder = optionsBuilde;
            this.MyOptionsBuilder = MyOptionsBuilder;
        }

        public long keyId => 820075;

        public object JsonToModel(string json,string deviceSN = null)
        {
            return System.Text.Json.JsonSerializer.Deserialize<AlarmJsonModel>(json); 
        }

        public async Task ReactData(object data, string Topic,string deviceSN = null)
        {
            try
            {
                AlarmJsonModel obj = data as AlarmJsonModel;
                if (data == null) return;
                if (Topic.IsNullOrEmpty()) return;
                if ("wsr".Equals(deviceSN))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tAlarmDeviceData_WSR.AddAsync(new tAlarmDeviceData_WSR
                        {
                            addTime = DateTime.Now,
                            Data = obj.alarm,
                            Topic = Topic
                        });
                        await db.SaveChangesAsync();
                    }

                }
                else if (deviceSN.Equals("YKF2"))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tAlarmDeviceData_YKF2.AddAsync(new tAlarmDeviceData_YKF2
                        {
                            addTime = DateTime.Now,
                            Data = obj.alarm,
                            Topic = Topic
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else if (deviceSN.Equals("YKF1"))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tAlarmDeviceData_YKF1.AddAsync(new tAlarmDeviceData_YKF1
                        {
                            addTime = DateTime.Now,
                            Data = obj.alarm,
                            Topic = Topic
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else if (deviceSN.Equals("XAFD"))
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tAlarmDeviceData_XAFD.AddAsync(new tAlarmDeviceData_XAFD
                        {
                            addTime = DateTime.Now,
                            Data = obj.alarm,
                            Topic = Topic
                        });
                        await db.SaveChangesAsync();
                    }
                }
                else
                {
                    using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                    {
                        await db.tAlarmDeviceData.AddAsync(new tAlarmDeviceData
                        {
                            addTime = DateTime.Now,
                            Data = obj.alarm,
                            Topic = Topic
                        });
                        await db.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine("tAlarmDeviceData:" + ex.Message);
            }
        }

    }
}
